-- SPDX-License-Identifier: GPL-3.0-or-later

local v_yonglan = fk.CreateSkill {
  name = "v_yonglan",
  tags = {  },
}

v_yonglan:addEffect("viewas", {
  anim_type = "defensive",
  pattern = "jink",
  card_filter = function(self, player, to_select, selected)
    if #selected == 1 then return false end
    return Fk:getCardById(to_select).number > player:getMark("v_yonglan")
      and Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 then
      return nil
    end
    local card = Fk:cloneCard("jink")
    card:addSubcard(cards[1])
    return card
  end,
  enabled_at_response = function(self, player)
    local res = false
    local cards = player:getCardIds("h")
    local card
    for _,p in ipairs(cards) do
      card = Fk:getCardById(p)
      if card.number > player:getMark("v_yonglan") then
        res = true
      end
    end
    return player:usedSkillTimes(v_yonglan.name, Player.HistoryTurn) < 1
      and res
  end,
})

v_yonglan:addEffect(fk.TargetConfirming, {
  can_refresh = function(self, event, target, player, data)
    return (target == player and player:hasSkill(v_yonglan.name))
  end,
  on_refresh = function(self, event, target, player, data)
    local card = data.card
    local room = player.room
    if card.trueName == "slash" and card.number > 0 and card.id > 0 then
      room:setPlayerMark(player, "v_yonglan", card.number)
    else
      room:setPlayerMark(player, "v_yonglan", 999)
    end
  end
})

return v_yonglan